<template>
  <div></div>
</template>

<script>
/*eslint-disable */
import pubsub from "pubsub-js";
import axios from "axios";
import url from "@/components/URL.json";
import { getCurrentInstance, onMounted } from "vue-demi";
import { useStore } from "vuex";

export default {
  name: "polygonCut",
  data() {
    return {
      publicList: [],
    };
  },
  setup() {
    let subList = [];
    const { proxy } = getCurrentInstance();
    const store = useStore();

    onMounted(() => {
      subList[0] = pubsub.subscribe("drawPolygon", (n, data) => {
        data = data[0];
        // 重组边界点
        let points = [];
        for (let i = 0; i < data.length; i += 3) {
          points.push([data[i], data[i + 1]]);
        }
        // 读取设施信息
        getPoiLocation().then((poi) => {
          // console.log(poi);
          let poiList = [];
          poi.forEach((element) => {
            element.forEach((e) => {
              poiList.push(e);
            });
          });
          // 网格框选
          let list = poiSelection(poiList, points);
          proxy.publicList = list;
          pubsub.publish("netPOI", list);
          store.commit("savePOI", list);
        });
      });
    });

    //读取POI信息
    function getPoiLocation() {
      let arr = [];
      let dbNameList = ["安防科技", "基础设施", "停车场", "重点场所"];
      return new Promise((reslove) => {
        for (let i = 0; i < dbNameList.length; i++) {
          axios({
            method: "get",
            url: url.basicURL + "getFacility",
            params: { db: "3d_" + dbNameList[i] },
          }).then((res) => {
            arr.push(res.data);
            if (arr.length === 4) reslove(arr);
          });
        }
      });
    }

    // 网格线裁剪POI
    function poiSelection(poiList, rangePoints) {
      rangePoints.push(rangePoints[0]);
      // 四至截取
      let enterpriseList = MaxAndMin(poiList, rangePoints);
      //   射线求交
      let selection = RayIntersection(enterpriseList, rangePoints);
      return selection;
    }

    // 四至截取
    function MaxAndMin(poiList, rangePoints) {
      let enterpriseList = [];
      //  读取四至
      let lngMax = 0;
      let lngMin = 9999999999999999;
      let latMax = 0;
      let latMin = 999999999999999;
      rangePoints.forEach((point) => {
        let lng = point[0];
        let lat = point[1];
        if (lng > lngMax) lngMax = lng;
        if (lng < lngMin) lngMin = lng;
        if (lat > latMax) latMax = lat;
        if (lat < latMin) latMin = lat;
      });
      //  读取四至内的企业点
      poiList.forEach((element) => {
        let x = Number(element["84Lng"]);
        let y = Number(element["84Lat"]);
        if (x < lngMax && x > lngMin && y < latMax && y > latMin) {
          enterpriseList.push(element);
        }
      });
      return enterpriseList;
    }

    // 射线求交
    function RayIntersection(enterpriseList, rangePoints) {
      let selection = [];
      // 遍历四至截取后的企业点,记录水平交点数量
      enterpriseList.forEach((element) => {
        let count = 0;
        let points = [];
        for (let i = 0; i < rangePoints.length - 1; i++) {
          //  确认当前边界点与前一边界点y值的高低
          let latMax, latMin;
          if (rangePoints[i][0] > rangePoints[i + 1][0]) {
            latMax = rangePoints[i][0];
            latMin = rangePoints[i + 1][0];
          } else {
            latMax = rangePoints[i + 1][0];
            latMin = rangePoints[i][0];
          }
          //   若企业点y值在当前边界点y值之间,则认为是相交的
          if (
            Number(element["84Lng"]) > latMin &&
            Number(element["84Lng"]) < latMax
          ) {
            // 更新相交计数
            count++;
            // 将当前边界点与前一边界点保存
            points.push([rangePoints[i], rangePoints[i + 1]]);
          }
        }
        // 若与边界无相交则跳过此企业
        if (count != 0) {
          let right = 0;
          let left = 0;
          points.forEach((point) => {
            let x = boundaryFunction(point, Number(element["84Lat"]));
            x > Number(element["84Lng"]) ? right++ : left++;
          });
          //   console.log(right, left);
          //  奇数偶数判断,若左右皆奇数则认为在选区内
          if (right % 2 != 0 && left % 2 != 0) {
            selection.push(element);
          }
        }
      });
      return selection;
    }

    //  求算边界线函数,根据边界函数求算相交点的x值
    function boundaryFunction(boundary, y) {
      let point1 = {
        y: boundary[0][1],
        x: boundary[0][0],
      };
      let point2 = {
        y: boundary[1][1],
        x: boundary[1][0],
      };
      // console.log(point1, point2);
      let top = point2.x * (y - point1.y) + point1.x * (point2.y - y);
      let bottom = point2.y - point1.y;
      let x = top / bottom;
      return x;
    }

    return { subList };
  },
  beforeRouteLeave() {
    pubsub.publish("deleteBufferPOI", this.publicList);
    pubsub.publish("firstPageShowChange", true);
    pubsub.publish("containerShowChange", false);
    pubsub.publish("needReturn", "网格");
    pubsub.publish("hierarchyQuit", "@");
    this.subList.forEach((element) => {
      pubsub.unsubscribe(element);
    });
  },
};
</script>

<style>
</style>